#include<bits/stdc++.h>
using uint=unsigned int;
using ll=long long;
using ull=unsigned long long;
using pii=std::pair<int,int>;
#define fst first
#define scd second
#define all(x) std::begin(x),std::end(x)
#define y0 y000000
#define y1 y111111
#define rep(i,s,t) for(int i=(s);i<=(t);i++)
#define irep(i,s,t) for(int i=(s);i>=(t);i--)
template<typename T> bool chmax(T &x,T const &y){
    return x<y?x=y,true:false;
}
template<typename T> bool chmin(T &x,T const &y){
    return y<x?x=y,true:false;
}
using namespace std;
template<typename T=int>
T read(){
    T x=0,f=1;
    char c=getchar();
    while(!isdigit(c)){
        f=c=='-'?-1:1;
        c=getchar();
    }
    while(isdigit(c)){
        x=x*10+(c-'0');
        c=getchar();
    }
    return x*f;
}
const int N=(1<<17)+10;
int lg[N*2];
int n,K,a[N*2];
ll sum_id[N*2];
int mx[N*2];
int deter[N*2];
// deter_id[N*2];
vector<pii> ask;
vector<bool> d[18];
namespace Gen{
    int a0[N];
    void read_a(){
        rep(i,1,n) a0[i]=read();
    }
    void gen_a(){
        int X[4];
        rep(i,0,3) X[i]=read();
        rep(i,1,n){
            a[i]=a0[i]^X[i%4];
        }
    }
}
using Gen::read_a; using Gen::gen_a;
bool fd(int o){
    int const R=lg[o];
    int const G=o-(1<<lg[o]);
    return d[R][G];
}
int curC;
bool determined(int o){
    return ((o<<K-lg[o])|((1<<K-lg[o])-1))<=curC+(1<<K)-1;
}
bool all_free(int o){
    return (o<<K-lg[o])>curC+(1<<K)-1;
}
int get_mx(int o){
    if(determined(o)) return deter[o];
    if(all_free(o)) return 32;
    return mx[o];
}
bool to_top(int top,int o,int mn){
    if(mn>top) return false;
    if(lg[o]==top) return true;
    if(!(o&1)){
        if(fd(o>>1) && get_mx(o^1)<=lg[o>>1]){
            return false;
        }
    }
    else{
        if(!fd(o>>1) && get_mx(o^1)<=lg[o<<1]){
            return false;
        }
    }
    return true;
}
vector<array<ll,18>> sum;
ll fre[N*2];
void get_sum(int o){
    if(all_free(o)){
        sum[o].fill(0);
        fre[o]=sum_id[o];
        return;
    }
    if(determined(o)){
        sum[o].fill(0);
        sum[o][a[deter[o]]]=deter[o];
        fre[o]=0;
        return;
    }
    int fr=o<<1,fo=o<<1|1;
    if(fd(o)){
        swap(fr,fo);
    }
    sum[o].fill(0);
    fre[o]=0;
    bool cfo=false;
    if(fre[fr]) {
        cfo=true;
        fre[o]+=fre[fr];
    }
    rep(x,0,17){
        if(!sum[fr][x]) continue;
        if(x>lg[o]){
            cfo=true;
        }
        else{
            sum[o][x]+=sum[fr][x];
        }
    }
    if(cfo){
        fre[o]+=fre[fo];
        rep(x,0,17) sum[o][x]+=sum[fo][x];
    }
}
void work(){

    rep(i,1,n){
        a[i]=K-a[i];
        // a[i]=0;
        // irep(t,K-1,0){
        //     int id=(1<<K-1)-(1<<t)+(i>>K-t)-(1<<t)+1;
        //     if(a[i]<id) {
        //         a[i]=t+1;
        //         break;
        //     }
        // }
    }
    rep(i,n+1,(1<<K)){
        a[i]=0;
    }
    rep(i,1,(1<<K)) deter[i+(1<<K)-1]=i;
    irep(i,(1<<K)-1,1){
        int l=i*2,r=i*2+1;
        if(fd(i)==0){
            if(a[deter[l]]<=lg[i]){
                deter[i]=deter[l];
            }
            else{
                deter[i]=deter[r];
            }
        }
        else{
            if(a[deter[r]]>lg[i]){
                deter[i]=deter[l];
            }
            else{
                deter[i]=deter[r];
            }
        }
    }
    ll ans=0;
    for(auto [C,coe]:ask){

        curC=C;
        int o=(1<<K)+C-1;
        while(o>1&&(o&1)) o>>=1;
        if((1<<lg[o]) == o) {
            ans^=1ll*coe*deter[o];
            continue;
        }
        get_sum(o);
        while(o>1){
            get_sum(o^1);
            get_sum(o>>1);
            o>>=1;
            // cout<<o<<":\n";
            // rep(i,0,17) cout<<sum[o][i]<<' ';
            // cout<<fre[o]<<'\n';
            if((1<<lg[o]) == o){
                ll cur=fre[o]+accumulate(all(sum[o]),0);
                ans^=1ll*coe*cur;

                break;
            }
        }
    }
    cout<<ans<<'\n';        
}
signed main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    lg[1]=0;
    rep(x,2,N*2-1) lg[x]=lg[x>>1]+1;
    int m;
    n=read(),m=read();
    K=lg[n*2-1];
    read_a();
    rep(i,1,m) ask.emplace_back(read(),i);
    irep(k,K-1,0){
        d[k].resize(1<<k);
        char c=getchar();
        while(!isdigit(c)) c=getchar();
        rep(i,0,(1<<k)-1){
            d[k][i]=c-'0';
            c=getchar();
        }
    }
    rep(i,0,(1<<K)-1){
        sum_id[(1<<K)+i]=i+1;
    }
    irep(i,(1<<K)-1,1){
        sum_id[i]=sum_id[i*2]+sum_id[i*2+1];
    }
    sum.resize((1<<K+1)+10);
    int T=read();
    while(T--) {
        gen_a();
        work();
    }
}